package com.zjpavt.client.page;

import com.zjpavt.client.hal.LedScreen;
import com.zjpavt.client.page.abstractpage.AbstractParentPage;
import com.zjpavt.client.page.basepage.UnknownPageSizeMultiPage;
import com.zjpavt.client.page.intefacepage.MultiPage;
import com.zjpavt.client.page.pageunit.ScreenLine;
import com.zjpavt.client.page.editableunit.EditUnitString;
import com.zjpavt.client.page.intefacepage.SonPage;
import com.zjpavt.client.util.builderfactory.BuilderFactory;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 第十五页
 * @author zyc
 */
@Slf4j
public class ConfigPage extends AbstractParentPage implements MultiPage {

    public static final int PAGE_LINE_NUM = 4;

    private UnknownPageSizeMultiPage unknownPageSizeMultiPage = new UnknownPageSizeMultiPage(this,getPageNum());
    /**
     *
     * @param pageNum
     * @param mRefreshPeriod
     */
    public ConfigPage(int pageNum,long mRefreshPeriod) {
        super(pageNum,mRefreshPeriod);
        DmxParameterPage dmxParameterPage = BuilderFactory.bulid(new DmxParameterPage(pageNum,this,0));
        sonPageList.add(dmxParameterPage);
        sonPageList.add(new PowerPage(pageNum,this,0));
        sonPageList.add(BuilderFactory.bulid(new DogWatchPage(pageNum,this,0)));
        sonPageList.add(BuilderFactory.bulid(new ElecConfigPage(pageNum,this,0)));
        sonPageList.add(new DoorStatusPage(pageNum,this,0));
        sonPageList.add(new TemperatureAndHumidityPage(pageNum, this, 0));
    }

    @Override
    public void initEditUnits() {
        addEditUnit(new EditUnitString(1, 1, 11, new String[] {(getEditUnitSize() + 1) + ".Art-Net"}));
        addEditUnit(new EditUnitString(2, 1, 11, new String[] {(getEditUnitSize() + 1) + ".Power"}));
        addEditUnit(new EditUnitString(3, 1, 11, new String[] {(getEditUnitSize() + 1) + ".WatchDog"}));
        addEditUnit(new EditUnitString(4, 1, 11, new String[] {(getEditUnitSize() + 1) + ".Elec"}));
        addEditUnit(new EditUnitString(1, 1, 11, new String[] {(getEditUnitSize() + 1) + ".Door"}));
        addEditUnit(new EditUnitString(2, 1, 15, new String[] {(getEditUnitSize() + 1) + ".TempHumidity"}));
    }

    @Override
    public void setDefaultVal() {
        throw new IllegalArgumentException();
    }

    @Override
    public void saveVal() {
        throw new IllegalArgumentException();
    }

    @Override
    public void refreshPage() {
        if (isVisitInnerPage()) {
            getCurrentActivePage().refreshPage();
            return;
        }
        List<ScreenLine> result = new ArrayList<>();
        for (int editUnitIndex = 0; editUnitIndex < PAGE_LINE_NUM; editUnitIndex++) {
            int currentLineBlinkIndex = getMultiPageNum() * PAGE_LINE_NUM + editUnitIndex;
            if (editUnitIndex ==0) {
                result.add(new ScreenLine.ScreenLineBuilder().text(getEditUnit(currentLineBlinkIndex).getText()).pageNum(getPageNum()).build());
                continue;
            } else {
                if (currentLineBlinkIndex < getEditUnitSize()) {
                    result.add(new ScreenLine.ScreenLineBuilder().text(getEditUnit(currentLineBlinkIndex).getText()).build());
                } else {
                    result.add(ScreenLine.BLANK);
                }
            }
        }
        LedScreen.getInstance().setText(result);
    }

    @Override
    public void onKeyDown() {
        if (isVisitInnerPage()) {
            getCurrentActivePage().onKeyDown();
        } else {
            super.onKeyDown();
        }
    }

    @Override
    public void onKeyUp() {
        if (isVisitInnerPage()) {
            getCurrentActivePage().onKeyUp();
        } else {
            super.onKeyUp();
        }
    }

    @Override
    public void onKeySet() {
        if (isVisitInnerPage()) {
            getCurrentActivePage().onKeySet();
        } else {
            super.onKeySet();
            if (getCurrBlinkIndex() % PAGE_LINE_NUM == 0) {
                change2Page(getCurrBlinkIndex() / 4);
            }
        }
    }

    /**
     * 当处于浏览下级页面的时候 直接转发
     *
     * 如果处于浏览模式无反馈
     * 如果处于编辑模式进入内嵌页面
     *
     */
    @Override
    public void onKeyEnt() {
        if (isVisitInnerPage()) {
            getCurrentActivePage().onKeyEnt();
            return;
        }
        if (isInEditMode()) {
            enterSonPage();
        } else {
            if (currBlinkIndex / PAGE_LINE_NUM == getPageSize() - 1) {
                currBlinkIndex = 0;
            } else {
                currBlinkIndex += PAGE_LINE_NUM;
            }
            change2Page(currBlinkIndex / PAGE_LINE_NUM);
        }
    }


    /**
     * 获取当前显示的内嵌页面
     * @return
     */
    @Override
    public SonPage getCurrentActivePage() {
        return currentActivePage;
    }

    @Override
    public void setCurrentActivePage(SonPage currentActivePage) {
        this.currentActivePage = currentActivePage;
    }

    @Override
    public void enterSonPage() {
        if (getCurrBlinkIndex() >= sonPageList.size()) {
            log.error("enter son page fail, index:" + getCurrBlinkIndex() + " bigger than pageSize");
            return;
        }
        SonPage sonPage = sonPageList.get(getCurrBlinkIndex());
        setCurrentActivePage(sonPage);
        exitEditMode();
        sonPage.enterSonPage();
    }

    @Override
    public void exitSonPage() {
        super.exitSonPage();
    }

    @Override
    public void exitEditMode() {
        super.exitEditMode();
        change2Page(0);
    }

    /**
     * @return 页码从0开始
     */
    @Override
    public int getMultiPageNum() {
        return unknownPageSizeMultiPage.getMultiPageNum();
    }

    @Override
    public int getPageSize() {
        int pageSize = (getEditUnitSize() - 1) / 4 + 1;
        return pageSize;
    }

    @Override
    public void change2Page(int pageNum) {
        unknownPageSizeMultiPage.change2Page(pageNum);
    }

    @Override
    public void change2NextPage() {
        unknownPageSizeMultiPage.change2NextPage();
    }

    @Override
    public void exitPage() {
        unknownPageSizeMultiPage.exitPage();
    }
}
